package com.sinodata.bsm.cicp.collector.IIS6;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class Status extends Collector {

    private static Logger logger = Logger.getLogger(Status.class);
    static final String cmdStr = "iisinfo.exe";
    static final String regStr = "(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmdStr, 0, 0);
            String line;
            Pattern p = Pattern.compile(regStr);
            Matcher match;
            while ((line = reader.readLine()) != null) {
                match = p.matcher(line);
                if (match.matches()) {
                    String cpuTime = dateFormat(match.group(1));
                    String cpuTotaleTime = dateFormat(match.group(2));
                    propValues[0] = ResPropertyValue.createPropValue(cpuTime, propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(cpuTotaleTime, propMetas[1]);
                    for (int i = 2; i < propMetas.length; i++) {
                        propValues[i] = ResPropertyValue.createPropValue(match.group(i + 1), propMetas[i]);
                    }
                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                    return values;
                }

            }

        } catch (IOException e) {
            logger.error("collect iis6 status error", e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }

        return null;
    }

    private String dateFormat(String str) {
        String regex = ".*(\\d+:\\d+:\\d+\\.\\d{4}).*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return str;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "" };
    }

    @Override
    public String[] getPropMeta() {
        //return new String[] { "CPU时间", "CPU总时间", "内存使用", "最大内存", "线程数", "IO读", "IO写" };
        //        return new String[] { "CPU_Time", "CPU_Total_Time", "Memory_Usage", "Max_Memory", "Thread_Count", "IO_Read", "IO_Write" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7" };
    }

}
